package com.simpleframe.util;

import java.util.Calendar;
import java.util.Date;
import java.util.Random;

/**
 * 随机数工具类
 * @author guojt
 * @since 2011-03-03
 *
 */
public class RandomUtil {
	
	/* 随机数的类型
	 * 0:数字随机数；1：字符随机数；2：数字字符混合随机数
	 */
	public final static int RANDOMTYPE_INT = 0;
	public final static int RANDOMTYPE_STRING = 1;
	public final static int RANDOMTYPE_INT_STRING = 2;
	
	final static String PREFIX = "tg";//uid前缀(推广员平台专用)
			
	/**
	 * 获得随机数
	 * @param length 需要获得随机数的长度
	 * @param type 随机数的类型
	 */
	public static String getRandomCode(int length, int type) {
		StringBuffer strRandom = new StringBuffer();// 随机字符串
		Random rnd = new Random();
		if (length < 0) {
			length = 8;
		}
		if ((type > 2) || (type < 0)) {
			type = 0;
		}
		switch (type) {
		case 0:
			for (int iLoop = 0; iLoop < length; iLoop++) {
				strRandom.append(Integer.toString(rnd.nextInt(10)));
			}
			break;
		case 1:
			for (int iLoop = 0; iLoop < length; iLoop++) {
				//把10到35之间的数字转成36进制表示的字符串
				strRandom.append(Integer.toString((10 + rnd.nextInt(26)), 36));
			}
			break;
		case 2:
			for (int iLoop = 0; iLoop < length; iLoop++) {
				//把0到35之间的数字转成36进制表示的字符串
				strRandom.append(Integer.toString(rnd.nextInt(36), 36));
			}
			break;
		}
		return strRandom.toString();
	}
	
	//获取年份的后两位，如2011，则取11
	private static String getYear(){ 
	     Calendar ca = Calendar.getInstance(); 
	     ca.setTime(new Date()); 
	     String year = "" + ca.get(Calendar.YEAR); 
	     
	     return year.substring(2); 
	} 
	
}